/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import org.lwjgl.util.Rectangle;

import fi.honkalampisaatio.lmodeller.LanguageResource;
import fi.honkalampisaatio.lmodeller.LModeller;
import fi.honkalampisaatio.lmodeller.Logger;
import fi.honkalampisaatio.lmodeller.ModelViewPage;
import fi.honkalampisaatio.lmodeller.blocks.BlockBox;
import fi.honkalampisaatio.lmodeller.blocks.BlockBuilder;
import fi.honkalampisaatio.lmodeller.blocks.BlockModel;
import fi.honkalampisaatio.lmodeller.selection.ModelSelectionEvent;
import fi.honkalampisaatio.lmodeller.selection.GameModeSelectionEvent.MODES;
import fi.honkalampisaatio.lmodeller.ui.GameButton.TEXTLOCATION;
import glapp.GLApp;



public class RandomSpinnerMenu extends RandomMenu implements ActionListener {
	GameTextLabel blockCountLabel;
	int blockCount;
	int maxBlockCount, minBlockCount;

	public RandomSpinnerMenu(int minBlockCount, int maxBlockCount)
	{
		this.minBlockCount = minBlockCount;
		this.maxBlockCount = maxBlockCount;
	}
	
    public void init()
    {
    	int h = GLApp.getHeight()/10;
    	int w = GLApp.getWidth()/10;
    	int allBlocksCount = BlockBox.getBlockBox().getCopyofAllBlocks().size();
    	maxBlockCount = (maxBlockCount>allBlocksCount)?allBlocksCount:maxBlockCount;
    	minBlockCount = (minBlockCount>maxBlockCount)?maxBlockCount:minBlockCount;
    	blockCount = minBlockCount;
    	GameButton upButton = new GameButton(new Rectangle(w*4,h*6,w*2,h*2), 
    			GameTextures.getInstance().getTexture("UP_BUTTON")
    			, LModeller.actionColor, null, TEXTLOCATION.UP);
    	GameButton downButton = new GameButton(new Rectangle(w*4,h*2,w*2,h*2), 
    			GameTextures.getInstance().getTexture("DOWN_BUTTON")
    			, LModeller.actionColor, null, TEXTLOCATION.UP);
    	GameButton okButton = new GameButton(new Rectangle(w*8,(int)(h*0.2),
    			(int)(w*1.8),(int)(h*1.4)), null, LModeller.actionColor, 
    			LanguageResource.getInstance().get("OK_BUTTON"), TEXTLOCATION.UP);
    	blockCountLabel = new GameTextLabel(new Rectangle(w*4,h*4,w*2,h*2),
    			(int)(Math.min(w*2, h*2)*0.8), LModeller.directionColor);
    	mode = MODES.RANDOM_STEP_GAME;
    	blockCountLabel.showFrame(true);
    	blockCountLabel.setText(String.valueOf(blockCount));
    	setUpLabelText(LanguageResource.getInstance().get("CHOOSE_BLOCK_COUNT"));

		setUpToolTipSentence(LanguageResource.getInstance().get("CHOOSE_BLOCK_COUNT"));
    	setInstructionSentence(LanguageResource.getInstance().get("CHOOSE_BLOCK_COUNT"));

    	super.addComponent(blockCountLabel);
    	super.addComponent(upButton);
    	upButton.addActionListener(this);
    	upButton.setActionCommand("UP_BUTTON");
    	super.addComponent(downButton);
    	downButton.addActionListener(this);
    	downButton.setActionCommand("DOWN_BUTTON");
    	super.addComponent(okButton);
    	okButton.addActionListener(this);
    	okButton.setActionCommand("OK_BUTTON");
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("UP_BUTTON"))
		{
			blockCount++;
			if(blockCount>maxBlockCount)
				blockCount=maxBlockCount;
	    	blockCountLabel.setText(String.valueOf(blockCount));
			Logger.getInstance().log("Random Menu: Up button selected, new block count is " + blockCount);
		}
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("DOWN_BUTTON"))
		{
			blockCount--;
			if(blockCount<minBlockCount)
				blockCount=minBlockCount;
	    	blockCountLabel.setText(String.valueOf(blockCount));
			Logger.getInstance().log("Random Menu: Down button selected, new block count is " + blockCount);
		}
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("OK_BUTTON"))
		{
			ArrayList<ModelViewPage> pages = new ArrayList<ModelViewPage>();
			BlockModel model = BlockBuilder.CreateRandomModel(blockCount);
			switch(mode)
			{
			case RANDOM_STEP_GAME:
				pages.add(new ModelViewPage(model, LanguageResource.getInstance().get("LOOK")));
				for(int i=0;i<model.getBlocks().size();i++)
				{
					BlockModel subModel = new BlockModel();
					//subModel.setName(model.getName());
					for(int idx = 0; idx<=i; idx++)
					{
						subModel.addBlock(model.getBlocks().get(idx));
					}
					subModel.processModel();
					pages.add(new ModelViewPage(subModel, LanguageResource.getInstance().get("BUILD") + " " + (i+1) + " / " 
							+ model.getBlocks().size(), LanguageResource.getInstance().get("BUILD")));
				}
				pages.add(new ModelViewPage(model, LanguageResource.getInstance().get("READY")));
				break;
			case MEMORY_GAME:
				pages.add(new ModelViewPage(model, LanguageResource.getInstance().get("MEMORIZE")));
				pages.add(new ModelViewPage(null, LanguageResource.getInstance().get("BUILD")));
				pages.add(new ModelViewPage(model, LanguageResource.getInstance().get("CHECK")));
				break;
			default:
				pages.add(new ModelViewPage(model, LanguageResource.getInstance().get("BUILD")));
				break;
			}
			ModelSelectionEvent se = new ModelSelectionEvent(this, 
					pages);
			Logger.getInstance().log("Random Menu: Ok button selected, block count is " + blockCount);
			this.fireSelectionEvent(se);
		}
	}
    
}
